import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // token: localStorage.getItem('token') || ''
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, newToken) {
      state.token = newToken
    },
    updateUserInfo(state, newUserInfo) {
      state.userInfo = newUserInfo
    }
  },
  actions: {
    async InitUserInfo(store) {
      console.log(store.state.token)
      const { data: res } = await axios.get('/my/userinfo')
      store.commit('updateUserInfo', res.data)
      console.log(res.data)
    }
  },
  modules: {}
})
